#!/bin/bash
# Author: Karol Krizka
# Last Modified: February 5, 2010
#
# Outputs the following line:
#  timestamp    revision    number-of-code-files    number-of-lines-in-code-files    number-of-header-files    size-of-project

# Configure the different suffixes
HEADER_SUFFIX=h
IMPLEMENTATION_SUFFIX=m

## CODE STARTS ##
NUM_HEADER_FILES=0
NUM_IMPLEMENTATION_FILES=0
NUM_LINES=0
SIZE=0

# Calculate number of lines and files of header type
for i in ${HEADER_SUFFIX}
do
    LOCAL_NUM_HEADER_FILES=`find . -name "*.${i}" | wc -l`
    NUM_HEADER_FILES=`expr ${NUM_HEADER_FILES} + ${LOCAL_NUM_HEADER_FILES}`
    HEADER_FILES=`find . -name "*.${i}"`
    
    if [ "x${HEADER_FILES}x" == "xx" ]
    then
	LOCAL_NUM_LINES=0
	LOCAL_SIZE=0
    else
	LOCAL_NUM_LINES=`cat $HEADER_FILES | wc -l`
# The total size of all files is the last field with the "file name" total. Run through the second awk to handle cases if an actual file named "title" exists
	LOCAL_SIZE=`du --total -s ${HEADER_FILES} | awk '/total/ { print $1}' | awk '{print $NF}'`
    fi

    NUM_LINES=`expr ${NUM_LINES} + ${LOCAL_NUM_LINES}`


    SIZE=`expr ${SIZE} + ${LOCAL_SIZE}`
done

# Calculate number of lines and files of implementation type
for i in ${IMPLEMENTATION_SUFFIX}
do
    LOCAL_NUM_IMPLEMENTATION_FILES=`find . -name "*.${i}" | wc -l`
    NUM_IMPLEMENTATION_FILES=`expr ${NUM_IMPLEMENTATION_FILES} + ${LOCAL_NUM_IMPLEMENTATION_FILES}`

    IMPLEMENTATION_FILES=`find . -name "*.${i}"`
    
    if [ "x${IMPLEMENTATION_FILES}x" == "xx" ]
    then
	LOCAL_NUM_LINES=0
	LOCAL_SIZE=0
    else
	LOCAL_NUM_LINES=`cat $IMPLEMENTATION_FILES | wc -l`
	LOCAL_SIZE=`du --total -s ${IMPLEMENTATION_FILES} | awk '/total/ { print $1}' | awk '{print $NF}'`
    fi
    NUM_LINES=`expr ${NUM_LINES} + ${LOCAL_NUM_LINES}`

    SIZE=`expr ${SIZE} + ${LOCAL_SIZE}`
done

NUM_CODE_FILES=`expr ${NUM_HEADER_FILES} + ${NUM_IMPLEMENTATION_FILES}`

# Calculate date
DATE=`svn info | awk '/Date/ { print $4}'`
TIME=`svn info | awk '/Date/ { print $5}'`
TZ=`svn info | awk '/Date/ { print $6}'`
UNIXTIMESTAMP=`date --utc -d "${DATE} ${TIME} ${TZ}" +%s`

SVNREVISION=`svn info | awk '/Revision/ {print $2}'`


echo -e $UNIXTIMESTAMP "\t" $SVNREVISION "\t" $NUM_CODE_FILES "\t" $NUM_LINES "\t" $NUM_HEADER_FILES "\t" $SIZE